// 导入仓库
import { useUserStore } from '@/store/user'
// 导入axios
import axios from 'axios'
import { ElMessage } from 'element-plus'
// 创建实例
const request = axios.create({
  //基础路径
  // baseURL: import.meta.env.VITE_APP_BASE_API, //基础路径上会携带/api
  baseURL: import.meta.env.VITE_SERVE, //基础路径上会携带/api
})
let store: any = null
// 请求的拦截
request.interceptors.request.use(
  (request) => {
    if (store === null) {
      store = useUserStore()
    }
    request.headers.Authorization = store.token
    return request
  },
  (error) => {
    ElMessage.error('网络连接异常了,稍后重试')
    return Promise.reject(new Error(error.message))
  },
)

// 响应的拦截
request.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    ElMessage.error('网络连接异常了,稍后重试')
    return Promise.reject(new Error(error.message))
  },
)

export default request
